TypeScript 'infer' ํค์๋์ ๋ํ ์ข ํฉ ๊ฐ์ด๋. ์กฐ๊ฑด๋ถ ํ์ ๊ณผ ํจ๊ป ์ฌ์ฉํ์ฌ ๊ฐ๋ ฅํ ํ์ ์ถ์ถ ๋ฐ ์กฐ์ ๋ฐฉ๋ฒ์ ์ค๋ช ํฉ๋๋ค. ๊ณ ๊ธ ์ฌ์ฉ ์ฌ๋ก ํฌํจ.
TypeScript infer ๋ง์คํฐํ๊ธฐ: ๊ณ ๊ธ ํ์ ์กฐ์์ ์ํ ์กฐ๊ฑด๋ถ ํ์ ์ถ์ถ
TypeScript์ ํ์
์์คํ
์ ๊ฐ๋ฐ์๊ฐ ๊ฒฌ๊ณ ํ๊ณ ์ ์ง๋ณด์ ๊ฐ๋ฅํ ์ ํ๋ฆฌ์ผ์ด์
์ ๊ตฌ์ถํ ์ ์๋๋ก ํ๋ ๋งค์ฐ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ์
๋๋ค. ์ด๋ฌํ ๊ฐ๋ ฅํจ์ ๊ฐ๋ฅํ๊ฒ ํ๋ ํต์ฌ ๊ธฐ๋ฅ ์ค ํ๋๋ ์กฐ๊ฑด๋ถ ํ์
๊ณผ ํจ๊ป ์ฌ์ฉ๋๋ infer
ํค์๋์
๋๋ค. ์ด ์กฐํฉ์ ๋ณต์กํ ํ์
๊ตฌ์กฐ์์ ํน์ ํ์
์ ์ถ์ถํ๋ ๋ฉ์ปค๋์ฆ์ ์ ๊ณตํฉ๋๋ค. ์ด ๋ธ๋ก๊ทธ ๊ฒ์๋ฌผ์์๋ infer
ํค์๋๋ฅผ ๊น์ด ํ๊ณ ๋ค์ด ๊ธฐ๋ฅ๊ณผ ๊ณ ๊ธ ์ฌ์ฉ ์ฌ๋ก๋ฅผ ์ค๋ช
ํฉ๋๋ค. API ์ํธ ์์ฉ๋ถํฐ ๋ณต์กํ ๋ฐ์ดํฐ ๊ตฌ์กฐ ์กฐ์์ ์ด๋ฅด๊ธฐ๊น์ง ๋ค์ํ ์ํํธ์จ์ด ๊ฐ๋ฐ ์๋๋ฆฌ์ค์ ์ ์ฉ ๊ฐ๋ฅํ ์ค์ฉ์ ์ธ ์์ ๋ฅผ ํ๊ตฌํ ๊ฒ์
๋๋ค.
์กฐ๊ฑด๋ถ ํ์ ์ด๋ ๋ฌด์์ธ๊ฐ์?
infer
์ ๋ํด ์์ธํ ์์๋ณด๊ธฐ ์ ์ ์กฐ๊ฑด๋ถ ํ์
์ ๊ฐ๋จํ ๋ณต์ตํด ๋ณด๊ฒ ์ต๋๋ค. TypeScript์ ์กฐ๊ฑด๋ถ ํ์
์ JavaScript์ ์ผํญ ์ฐ์ฐ์์ ์ ์ฌํ๊ฒ ์กฐ๊ฑด์ ๋ฐ๋ผ ํ์
์ ์ ์ํ ์ ์๋๋ก ํฉ๋๋ค. ๊ธฐ๋ณธ ๊ตฌ๋ฌธ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
T extends U ? X : Y
์ด๊ฒ์ "๋ง์ฝ ํ์
T
๊ฐ ํ์
U
์ ํ ๋น ๊ฐ๋ฅํ๋ค๋ฉด, ํ์
์ X
์ด๊ณ , ๊ทธ๋ ์ง ์์ผ๋ฉด ํ์
์ Y
์
๋๋ค."๋ผ๊ณ ์ฝ์ต๋๋ค.
์์:
type IsString<T> = T extends string ? true : false;
type StringResult = IsString<string>; // type StringResult = true
type NumberResult = IsString<number>; // type NumberResult = false
infer
ํค์๋ ์๊ฐ
infer
ํค์๋๋ ์กฐ๊ฑด๋ถ ํ์
์ extends
์ ๋ด์์ ์ฌ์ฉ๋์ด ๊ฒ์ฌ๋๋ ํ์
์ผ๋ก๋ถํฐ ์ถ๋ก ๋ ์ ์๋ ํ์
๋ณ์๋ฅผ ์ ์ธํฉ๋๋ค. ๋ณธ์ง์ ์ผ๋ก๋ ๋์ค์ ์ฌ์ฉํ๊ธฐ ์ํด ํ์
์ ์ผ๋ถ๋ฅผ "์บก์ฒ"ํ ์ ์๊ฒ ํฉ๋๋ค.
๊ธฐ๋ณธ ๊ตฌ๋ฌธ:
type MyType<T> = T extends (infer U) ? U : never;
์ด ์์์์ T
๊ฐ ์ด๋ค ํ์
์ ํ ๋น ๊ฐ๋ฅํ๋ค๋ฉด, TypeScript๋ U
์ ํ์
์ ์ถ๋ก ํ๋ ค๊ณ ์๋ํ ๊ฒ์
๋๋ค. ์ถ๋ก ์ด ์ฑ๊ณตํ๋ฉด ํ์
์ U
๊ฐ ๋๊ณ , ๊ทธ๋ ์ง ์์ผ๋ฉด never
๊ฐ ๋ฉ๋๋ค.
infer
์ ๊ฐ๋จํ ์์
1. ํจ์์ ๋ฐํ ํ์ ์ถ๋ก
์ผ๋ฐ์ ์ธ ์ฌ์ฉ ์ฌ๋ก๋ ํจ์์ ๋ฐํ ํ์ ์ ์ถ๋ก ํ๋ ๊ฒ์ ๋๋ค.
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
function add(a: number, b: number): number {
return a + b;
}
type AddReturnType = ReturnType<typeof add>; // type AddReturnType = number
function greet(name: string): string {
return `Hello, ${name}!`;
}
type GreetReturnType = ReturnType<typeof greet>; // type GreetReturnType = string
์ด ์์์์ ReturnType<T>
๋ ํจ์ ํ์
T
๋ฅผ ์
๋ ฅ์ผ๋ก ๋ฐ์ต๋๋ค. T
๊ฐ ์ด๋ค ์ธ์๋ฅผ ๋ฐ๋ ๊ฐ์ ๋ฐํํ๋ ํจ์์ ํ ๋น ๊ฐ๋ฅํ์ง ํ์ธํฉ๋๋ค. ๊ทธ๋ ๋ค๋ฉด ๋ฐํ ํ์
์ R
๋ก ์ถ๋ก ํ์ฌ ๋ฐํํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด any
๋ฅผ ๋ฐํํฉ๋๋ค.
2. ๋ฐฐ์ด ์์ ํ์ ์ถ๋ก
๋ ๋ค๋ฅธ ์ ์ฉํ ์๋๋ฆฌ์ค๋ ๋ฐฐ์ด์์ ์์ ํ์ ์ ์ถ์ถํ๋ ๊ฒ์ ๋๋ค.
type ArrayElementType<T> = T extends (infer U)[] ? U : never;
type NumberArrayType = ArrayElementType<number[]>; // type NumberArrayType = number
type StringArrayType = ArrayElementType<string[]>; // type StringArrayType = string
type MixedArrayType = ArrayElementType<(string | number)[]>; // type MixedArrayType = string | number
type NotAnArrayType = ArrayElementType<number>; // type NotAnArrayType = never
์ฌ๊ธฐ์ ArrayElementType<T>
๋ T
๊ฐ ๋ฐฐ์ด ํ์
์ธ์ง ํ์ธํฉ๋๋ค. ๋ฐฐ์ด์ด๋ผ๋ฉด ์์ ํ์
์ U
๋ก ์ถ๋ก ํ์ฌ ๋ฐํํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด never
๋ฅผ ๋ฐํํฉ๋๋ค.
infer
์ ๊ณ ๊ธ ์ฌ์ฉ ์ฌ๋ก
1. ์์ฑ์ ๋งค๊ฐ๋ณ์ ์ถ๋ก
infer
๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ์ ํจ์์ ๋งค๊ฐ๋ณ์ ํ์
์ ์ถ์ถํ ์ ์์ต๋๋ค.
type ConstructorParameters<T extends new (...args: any) => any> = T extends new (...args: infer P) => any ? P : never;
class Person {
constructor(public name: string, public age: number) {}
}
type PersonConstructorParams = ConstructorParameters<typeof Person>; // type PersonConstructorParams = [string, number]
class Point {
constructor(public x: number, public y: number) {}
}
type PointConstructorParams = ConstructorParameters<typeof Point>; // type PointConstructorParams = [number, number]
์ด ๊ฒฝ์ฐ ConstructorParameters<T>
๋ ์์ฑ์ ํจ์ ํ์
T
๋ฅผ ๋ฐ์ต๋๋ค. ์์ฑ์ ๋งค๊ฐ๋ณ์์ ํ์
์ P
๋ก ์ถ๋ก ํ์ฌ ํํ๋ก ๋ฐํํฉ๋๋ค.
2. ๊ฐ์ฒด ํ์ ์์ ์์ฑ ์ถ์ถ
infer
๋ ๋งคํ๋ ํ์
๊ณผ ์กฐ๊ฑด๋ถ ํ์
์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด ํ์
์์ ํน์ ์์ฑ์ ์ถ์ถํ๋ ๋ฐ์๋ ์ฌ์ฉํ ์ ์์ต๋๋ค.
type PickByType<T, K extends keyof T, U> = {
[P in K as T[P] extends U ? P : never]: T[P];
};
interface User {
id: number;
name: string;
age: number;
email: string;
isActive: boolean;
}
type StringProperties = PickByType<User, keyof User, string>; // type StringProperties = { name: string; email: string; }
type NumberProperties = PickByType<User, keyof User, number>; // type NumberProperties = { id: number; age: number; }
//์ง๋ฆฌ ์ขํ๋ฅผ ๋ํ๋ด๋ ์ธํฐํ์ด์ค.
interface GeoCoordinates {
latitude: number;
longitude: number;
altitude: number;
country: string;
city: string;
timezone: string;
}
type NumberCoordinateProperties = PickByType<GeoCoordinates, keyof GeoCoordinates, number>; // type NumberCoordinateProperties = { latitude: number; longitude: number; altitude: number; }
์ฌ๊ธฐ์ PickByType<T, K, U>
๋ T
์ ์์ฑ(K
์ ํค๊ฐ ์๋) ์ค ๊ฐ์ด ํ์
U
์ ํ ๋น ๊ฐ๋ฅํ ๊ฒ๋ค๋ง ํฌํจํ๋ ์ ํ์
์ ์์ฑํฉ๋๋ค. ๋งคํ๋ ํ์
์ T
์ ํค๋ฅผ ๋ฐ๋ณตํ๊ณ , ์กฐ๊ฑด๋ถ ํ์
์ ์ง์ ๋ ํ์
๊ณผ ์ผ์นํ์ง ์๋ ํค๋ฅผ ํํฐ๋งํฉ๋๋ค.
3. Promise์ ํจ๊ป ์์ ํ๊ธฐ
Promise
์ ํด๊ฒฐ๋ ํ์
์ ์ถ๋ก ํ ์ ์์ต๋๋ค.
type Awaited<T> = T extends Promise<infer U> ? U : T;
async function fetchData(): Promise<string> {
return 'Data from API';
}
type FetchDataType = Awaited<ReturnType<typeof fetchData>>; // type FetchDataType = string
async function fetchNumbers(): Promise<number[]> {
return [1, 2, 3];
}
type FetchedNumbersType = Awaited<ReturnType<typeof fetchNumbers>>; //type FetchedNumbersType = number[]
Awaited<T>
ํ์
์ Promise
์ผ ๊ฒ์ผ๋ก ์์๋๋ ํ์
T
๋ฅผ ๋ฐ์ต๋๋ค. ๊ทธ๋ฐ ๋ค์ Promise
์ ํด๊ฒฐ๋ ํ์
U
๋ฅผ ์ถ๋ก ํ์ฌ ๋ฐํํฉ๋๋ค. T
๊ฐ promise๊ฐ ์๋๋ฉด T๋ฅผ ๋ฐํํฉ๋๋ค. ์ด๊ฒ์ ์ต์ ๋ฒ์ ์ TypeScript์ ๋ด์ฅ๋ ์ ํธ๋ฆฌํฐ ํ์
์
๋๋ค.
4. Promise ๋ฐฐ์ด ํ์ ์ถ์ถ
Awaited
์ ๋ฐฐ์ด ํ์
์ถ๋ก ์ ๊ฒฐํฉํ๋ฉด Promise ๋ฐฐ์ด์ ์ํด ํด๊ฒฐ๋ ํ์
์ ์ถ๋ก ํ ์ ์์ต๋๋ค. Promise.all
์ ๋ค๋ฃฐ ๋ ํนํ ์ ์ฉํฉ๋๋ค.
type PromiseArrayReturnType<T extends Promise<any>[]> = {
[K in keyof T]: Awaited<T[K]>;
};
async function getUSDRate(): Promise<number> {
return 0.0069;
}
async function getEURRate(): Promise<number> {
return 0.0064;
}
const rates = [getUSDRate(), getEURRate()];
type RatesType = PromiseArrayReturnType<typeof rates>;
// type RatesType = [number, number]
์ด ์์๋ ๋จผ์ ๋ ๊ฐ์ ๋น๋๊ธฐ ํจ์์ธ getUSDRate
์ getEURRate
๋ฅผ ์ ์ํ์ฌ ํ์จ์ ๊ฐ์ ธ์ค๋ ๊ฒ์ ์๋ฎฌ๋ ์ด์
ํฉ๋๋ค. ๊ทธ๋ฐ ๋ค์ PromiseArrayReturnType
์ ํธ๋ฆฌํฐ ํ์
์ ๋ฐฐ์ด์ ๊ฐ Promise
์์ ํด๊ฒฐ๋ ํ์
์ ์ถ์ถํ์ฌ ํด๋น Promise์ ํด๋น ํ์
์ awaited ํ์
์ธ ๊ฐ ์์๋ฅผ ๊ฐ์ง ํํ ํ์
์ด ๋ฉ๋๋ค.
๋ค์ํ ๋๋ฉ์ธ์ ๊ฑธ์น ์ค์ฉ์ ์ธ ์์
1. ์ ์์๊ฑฐ๋ ์ ํ๋ฆฌ์ผ์ด์
API์์ ์ ํ ์ธ๋ถ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ ์ ์์๊ฑฐ๋ ์ ํ๋ฆฌ์ผ์ด์
์ ๊ณ ๋ คํด ๋ณด์ธ์. infer
๋ฅผ ์ฌ์ฉํ์ฌ ์ ํ ๋ฐ์ดํฐ์ ํ์
์ ์ถ์ถํ ์ ์์ต๋๋ค.
interface Product {
id: number;
name: string;
price: number;
description: string;
imageUrl: string;
category: string;
rating: number;
countryOfOrigin: string;
}
async function fetchProduct(productId: number): Promise<Product> {
// API ํธ์ถ ์๋ฎฌ๋ ์ด์
return new Promise((resolve) => {
setTimeout(() => {
resolve({
id: productId,
name: 'Example Product',
price: 29.99,
description: 'A sample product',
imageUrl: 'https://example.com/image.jpg',
category: 'Electronics',
rating: 4.5,
countryOfOrigin: 'Canada'
});
}, 500);
});
}
type ProductType = Awaited<ReturnType<typeof fetchProduct>>; // type ProductType = Product
function displayProductDetails(product: ProductType) {
console.log(`Product Name: ${product.name}`);
console.log(`Price: ${product.price} ${product.countryOfOrigin === 'Canada' ? 'CAD' : (product.countryOfOrigin === 'USA' ? 'USD' : 'EUR')}`);
}
fetchProduct(123).then(displayProductDetails);
์ด ์์์์๋ Product
์ธํฐํ์ด์ค์ API์์ ์ ํ ์ธ๋ถ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋ fetchProduct
ํจ์๋ฅผ ์ ์ํฉ๋๋ค. Awaited
์ ReturnType
์ ์ฌ์ฉํ์ฌ fetchProduct
ํจ์์ ๋ฐํ ํ์
์์ Product
ํ์
์ ์ถ์ถํ์ฌ displayProductDetails
ํจ์๋ฅผ ํ์
๊ฒ์ฌํ ์ ์๋๋ก ํฉ๋๋ค.
2. ๊ตญ์ ํ (i18n)
๋ก์ผ์ผ์ ๋ฐ๋ผ ๋ค๋ฅธ ๋ฌธ์์ด์ ๋ฐํํ๋ ๋ฒ์ญ ํจ์๊ฐ ์๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค. infer
๋ฅผ ์ฌ์ฉํ์ฌ ์ด ํจ์์ ๋ฐํ ํ์
์ ์ถ์ถํ์ฌ ํ์
์์ ์ฑ์ ํ๋ณดํ ์ ์์ต๋๋ค.
interface Translations {
greeting: string;
farewell: string;
welcomeMessage: (name: string) => string;
}
const enTranslations: Translations = {
greeting: 'Hello',
farewell: 'Goodbye',
welcomeMessage: (name: string) => `Welcome, ${name}!`,
};
const frTranslations: Translations = {
greeting: 'Bonjour',
farewell: 'Au revoir',
welcomeMessage: (name: string) => `Bienvenue, ${name}!`,
};
function getTranslation(locale: 'en' | 'fr'): Translations {
return locale === 'en' ? enTranslations : frTranslations;
}
type TranslationType = ReturnType<typeof getTranslation>;
function greetUser(locale: 'en' | 'fr', name: string) {
const translations = getTranslation(locale);
console.log(translations.welcomeMessage(name));
}
greetUser('fr', 'Jean'); // Output: Bienvenue, Jean!
์ฌ๊ธฐ์ TranslationType
์ Translations
์ธํฐํ์ด์ค๋ก ์ถ๋ก ๋์ด greetUser
ํจ์๊ฐ ๋ฒ์ญ๋ ๋ฌธ์์ด์ ์ก์ธ์คํ๊ธฐ ์ํ ์ฌ๋ฐ๋ฅธ ํ์
์ ๋ณด๋ฅผ ๊ฐ๋๋ก ํฉ๋๋ค.
3. API ์๋ต ์ฒ๋ฆฌ
API์ ์์
ํ ๋ ์๋ต ๊ตฌ์กฐ๋ ๋ณต์กํ ์ ์์ต๋๋ค. infer
๋ ์ค์ฒฉ๋ API ์๋ต์์ ํน์ ๋ฐ์ดํฐ ํ์
์ ์ถ์ถํ๋ ๋ฐ ๋์์ด ๋ ์ ์์ต๋๋ค.
interface ApiResponse<T> {
status: number;
data: T;
message?: string;
}
interface UserData {
id: number;
username: string;
email: string;
profile: {
firstName: string;
lastName: string;
country: string;
language: string;
}
}
async function fetchUser(userId: number): Promise<ApiResponse<UserData>> {
// API ํธ์ถ ์๋ฎฌ๋ ์ด์
return new Promise((resolve) => {
setTimeout(() => {
resolve({
status: 200,
data: {
id: userId,
username: 'johndoe',
email: 'john.doe@example.com',
profile: {
firstName: 'John',
lastName: 'Doe',
country: 'USA',
language: 'en'
}
}
});
}, 500);
});
}
type UserApiResponse = Awaited<ReturnType<typeof fetchUser>>;
type UserProfileType = UserApiResponse['data']['profile'];
function displayUserProfile(profile: UserProfileType) {
console.log(`Name: ${profile.firstName} ${profile.lastName}`);
console.log(`Country: ${profile.country}`);
}
fetchUser(123).then((response) => {
if (response.status === 200) {
displayUserProfile(response.data.profile);
}
});
์ด ์์์์๋ ApiResponse
์ธํฐํ์ด์ค์ UserData
์ธํฐํ์ด์ค๋ฅผ ์ ์ํฉ๋๋ค. infer
์ ํ์
์ธ๋ฑ์ฑ์ ์ฌ์ฉํ์ฌ API ์๋ต์์ UserProfileType
์ ์ถ์ถํ์ฌ displayUserProfile
ํจ์๊ฐ ์ฌ๋ฐ๋ฅธ ํ์
์ ๋ฐ๋๋ก ํฉ๋๋ค.
infer
์ฌ์ฉ ๋ชจ๋ฒ ์ฌ๋ก
- ๋จ์ํ๊ฒ ์ ์ง:
infer
๋ ํ์ํ ๋๋ง ์ฌ์ฉํ์ญ์์ค. ๊ณผ๋ํ ์ฌ์ฉ์ ์ฝ๋๋ฅผ ์ฝ๊ณ ์ดํดํ๊ธฐ ์ด๋ ต๊ฒ ๋ง๋ค ์ ์์ต๋๋ค. - ํ์
๋ฌธ์ํ: ์กฐ๊ฑด๋ถ ํ์
๋ฐ
infer
๋ฌธ์ด ๋ฌด์์ ํ๊ณ ์๋์ง ์ค๋ช ํ๋ ์ฃผ์์ ์ถ๊ฐํ์ญ์์ค. - ํ์ ํ ์คํธ: TypeScript์ ํ์ ๊ฒ์ฌ๋ฅผ ์ฌ์ฉํ์ฌ ํ์ ์ด ์์๋๋ก ์๋ํ๋์ง ํ์ธํ์ญ์์ค.
- ์ฑ๋ฅ ๊ณ ๋ ค: ๋ณต์กํ ์กฐ๊ฑด๋ถ ํ์ ์ ๋๋๋ก ์ปดํ์ผ ์๊ฐ์ ์ํฅ์ ๋ฏธ์น ์ ์์ต๋๋ค. ํ์ ์ ๋ณต์ก์ฑ์ ์ผ๋์ ๋์ญ์์ค.
- ์ ํธ๋ฆฌํฐ ํ์
์ฌ์ฉ: TypeScript๋ ์ฌ์ฉ์ ์ง์
infer
๋ฌธ ์์ด๋ ๋ง์ ์ผ๋ฐ์ ์ธ ํ์ ์กฐ์ ์์ ์ ์ฒ๋ฆฌํ ์ ์๋ ์ฌ๋ฌ ๊ธฐ๋ณธ ์ ํธ๋ฆฌํฐ ํ์ ์ ์ ๊ณตํฉ๋๋ค(์:ReturnType
,Awaited
).
์ผ๋ฐ์ ์ธ ํจ์
- ์๋ชป๋ ์ถ๋ก : ๋๋๋ก TypeScript๋ ์์๊ณผ ๋ค๋ฅธ ํ์ ์ ์ถ๋ก ํ ์ ์์ต๋๋ค. ํ์ ์ ์ ๋ฐ ์กฐ๊ฑด์ ๋ค์ ํ์ธํ์ญ์์ค.
- ์ํ ์ข
์์ฑ:
infer
๋ฅผ ์ฌ์ฉํ ์ฌ๊ท ํ์ ์ ์ ์ ์ฃผ์ํ์ญ์์ค. ์ํ ์ข ์์ฑ ๋ฐ ์ปดํ์ผ ์ค๋ฅ๋ก ์ด์ด์ง ์ ์์ต๋๋ค. - ์ง๋์น๊ฒ ๋ณต์กํ ํ์ : ์ดํดํ๊ณ ์ ์ง ๊ด๋ฆฌํ๊ธฐ ์ด๋ ค์ด ์ง๋์น๊ฒ ๋ณต์กํ ์กฐ๊ฑด๋ถ ํ์ ์ ํผํ์ญ์์ค. ๋ ์๊ณ ๊ด๋ฆฌํ๊ธฐ ์ฌ์ด ํ์ ์ผ๋ก ๋ถํดํ์ญ์์ค.
infer
์ ๋์
infer
๋ ๊ฐ๋ ฅํ ๋๊ตฌ์ด์ง๋ง ๋์์ ์ธ ์ ๊ทผ ๋ฐฉ์์ด ๋ ์ ํฉํ ๊ฒฝ์ฐ๊ฐ ์์ต๋๋ค.
- ํ์ ๋จ์ธ: ๊ฒฝ์ฐ์ ๋ฐ๋ผ ํ์ ๋จ์ธ์ ์ฌ์ฉํ์ฌ ํ์ ์ ์ถ๋ก ํ๋ ๋์ ๊ฐ์ ํ์ ์ ๋ช ์์ ์ผ๋ก ์ง์ ํ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ ํ์ ๋จ์ธ์ ํ์ ๊ฒ์ฌ๋ฅผ ์ฐํํ ์ ์์ผ๋ฏ๋ก ์ฃผ์ํ์ญ์์ค.
- ํ์ ๊ฐ๋: ํ์ ๊ฐ๋๋ ๋ฐํ์ ๊ฒ์ฌ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๊ฐ์ ํ์ ์ ์ขํ๋ ๋ฐ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ด๋ ๋ฐํ์ ์กฐ๊ฑด์ ๋ฐ๋ผ ๋ค๋ฅธ ํ์ ์ ์ฒ๋ฆฌํด์ผ ํ ๋ ์ ์ฉํฉ๋๋ค.
- ์ ํธ๋ฆฌํฐ ํ์
: TypeScript๋ ์ฌ์ฉ์ ์ง์
infer
๋ฌธ ์์ด๋ ๋ง์ ์ผ๋ฐ์ ์ธ ํ์ ์กฐ์ ์์ ์ ์ฒ๋ฆฌํ ์ ์๋ ํ๋ถํ ์ ํธ๋ฆฌํฐ ํ์ ์ ์ ๊ณตํฉ๋๋ค.
๊ฒฐ๋ก
TypeScript์ infer
ํค์๋๋ ์กฐ๊ฑด๋ถ ํ์
๊ณผ ๊ฒฐํฉ๋ ๋ ๊ณ ๊ธ ํ์
์กฐ์ ๊ธฐ๋ฅ์ ํ์ฉํฉ๋๋ค. ์ด๋ฅผ ํตํด ๋ณต์กํ ํ์
๊ตฌ์กฐ์์ ํน์ ํ์
์ ์ถ์ถํ์ฌ ๋ ๊ฒฌ๊ณ ํ๊ณ ์ ์ง๋ณด์ ๊ฐ๋ฅํ๋ฉฐ ํ์
์์ ํ ์ฝ๋๋ฅผ ์์ฑํ ์ ์์ต๋๋ค. ํจ์ ๋ฐํ ํ์
์ถ๋ก ๋ถํฐ ๊ฐ์ฒด ํ์
์์ฑ ์ถ์ถ์ ์ด๋ฅด๊ธฐ๊น์ง ๊ฐ๋ฅ์ฑ์ ๋ฌด๊ถ๋ฌด์งํฉ๋๋ค. ์ด ๊ฐ์ด๋์ ์ค๋ช
๋ ์์น๊ณผ ๋ชจ๋ฒ ์ฌ๋ก๋ฅผ ์ดํดํจ์ผ๋ก์จ infer
๋ฅผ ์ต๋ํ ํ์ฉํ๊ณ TypeScript ๊ธฐ์ ์ ํฅ์์ํฌ ์ ์์ต๋๋ค. ํ์
๋ฌธ์๋ฅผ ์์ฑํ๊ณ ์ฒ ์ ํ ํ
์คํธํ๋ฉฐ ์ ์ ํ ๊ฒฝ์ฐ ๋์์ ์ธ ์ ๊ทผ ๋ฐฉ์์ ๊ณ ๋ คํ์ญ์์ค. infer
๋ง์คํฐ๋ ์ง์ ์ผ๋ก ํํ๋ ฅ ์๊ณ ๊ฐ๋ ฅํ TypeScript ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋๋ก ํ์ฌ ๊ถ๊ทน์ ์ผ๋ก ๋ ๋์ ์ํํธ์จ์ด๋ก ์ด์ด์ง๋๋ค.